
package cz.czu.pef.DataModelling.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.richfaces.component.SortOrder;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.czu.pef.common.CustomFilterField;
import cz.czu.pef.common.CustomSortField;

@Component
@Configurable
@Scope(value = "prototype")
public abstract class PagingDataProvider<T> implements DataProvider<T>
{
	private static final long serialVersionUID = 5015665477629397068L;
	private static final Logger logger = Logger.getLogger(PagingDataProvider.class.getName());

	private List<CustomFilterField> customFilterFields;
	private List<CustomSortField> customSortFields = new ArrayList<CustomSortField>();
	private Locale locale;

	@Override
	public void setCustomFilterFields(Map<String, String> filterValues)
	{
		this.customFilterFields = new ArrayList<CustomFilterField>();

		if (filterValues != null)
		{
			for (String key : filterValues.keySet())
			{
				if (key == null || key.trim().equals(""))
					continue;
				String filterValue = filterValues.get(key);
				if (logger.isLoggable(Level.FINEST))
				{
					String buffer = "setCustomFilterFields():: Processing key(" + key + ") = " + filterValue;
					logger.finest(buffer);
				}
				if (filterValue != null && !"".equals(filterValue.trim())) // skip empty filters
				{
					if (!filterValue.startsWith("%"))
						filterValue = "%" + filterValue;

					if (!filterValue.endsWith("%"))
						filterValue = filterValue + "%";

					CustomFilterField newField = new CustomFilterField();
					newField.setPropertyName(key);
					newField.setFilterValue(filterValue);
					customFilterFields.add(newField);
				}
			}
		}
	}

	/**
	 * Gets sort values usable in non-web application area (uses no JSF/RichFaces classes)
	 * 
	 * @return
	 */
	@Override
	public List<CustomSortField> getCustomSortFields()
	{
		return customSortFields;
	}

	@Override
	public void setCustomSortFields(Map<String, SortOrder> sortsOrders)
	{
		customSortFields = new ArrayList<CustomSortField>();

		if (sortsOrders != null)
		{
			for (String key : sortsOrders.keySet())
			{
				if (key == null || key.trim().equals(""))
					continue;
				SortOrder sortOrder = sortsOrders.get(key);
				CustomSortField newField = new CustomSortField();
				if (SortOrder.ascending.equals(sortOrder))
				{
					newField.setOrdering(CustomSortField.ASCENDING);
				}
				else if (SortOrder.descending.equals(sortOrder))
				{
					newField.setOrdering(CustomSortField.DESCENDING);
				}
				else
				{
					// UNORDERED => skip it
					continue;
				}

				newField.setPropertyName(key);
				if (logger.isLoggable(Level.FINEST))
				{
					String buffer = "setSortFields():: key(" + key + ") = " + newField.getOrdering();
					logger.finest(buffer);
				}
				customSortFields.add(newField);
			}
		}
	}

	/**
	 * Gets filter values usable in non-web application area (uses no JSF/RichFaces classes)
	 * 
	 * @return
	 */
	@Override
	public List<CustomFilterField> getCustomFilterFields()
	{
		return customFilterFields;
	}

	@Override
	public void setLocale(Locale locale)
	{
		if (locale != null)
			this.locale = locale;
		else
			this.locale = Locale.ENGLISH; // default
	}

	@Override
	public Locale getLocale()
	{
		return this.locale;
	}
}